//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_KMUTEX_H__
#define __ELASTOS_KMUTEX_H__

//
// KMutex
//
#ifdef _DEBUG
#define DEBUG_KMUTEX
#endif //_DEBUG
class KMutex : public SyncObject
{
    friend class Thread;

public:
    bool_t IsLocked() const
    {
        return (NULL != m_pOwner);
    }

    bool_t IsHeld() const;

    void Lock();

    void Unlock();

public:
    KMutex()
    {
        m_uNested = 0;
    }
#ifdef DEBUG_KMUTEX
    KMutex(char *p, Int32 nLine)
    {
        m_uNested = 0;
        memcpy(m_pFilepath, p, strlen(p)+1);
        m_nLine = nLine;
    }
#endif //DEBUG_KMUTEX
    void _Lock();

    void _Unlock();

private:
    void Initialize()
    {
        SyncObject::Initialize();
        m_uNested = 0;
    }

private:
    uint_t      m_uNested;
#ifdef DEBUG_KMUTEX
    uint_t      m_nLine;
    char        m_pFilepath[260];
#endif //DEBUG_KMUTEX
};

#endif //__ELASTOS_KMUTEX_H__
